﻿using System;
using System.Linq;
using System.Data.Entity;
using Gozib.Data.Contracts;

namespace Gozib.Data
{
  public class EFRepository<T> : IRepository<T> where T : class
  {
    protected DbContext DbContext { get; set; }
    protected DbSet<T> DbSet { get; set; }

    public EFRepository(DbContext dbContext)
    {
      if (dbContext == null)
      {
        throw new Exception("dbContext");
      }

      DbContext = dbContext;
      DbSet = DbContext.Set<T>();
    }

    public IQueryable<T> GetAll()
    {
      return DbSet;
    }

    public IQueryable<T> Find(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
    {
      return DbSet.Where(predicate);
    }

    public T GetById(int id)
    {
      return DbSet.Find(id);
    }

    public void Remove(T entity)
    {
      DbSet.Remove(entity);
    }

    public void Add(T entity)
    {
      DbSet.Add(entity);
    }
  }
}
